<template>
  <div class="bili-header__channel">
    <div class="primary-menu">
      <div class="main-link" @click="navigateTo('/')">
        <i class="iconfont icon-shouyeweixuanzhong"/>
        <span>首页</span>
      </div>
    </div>

    <div class="channel-tags" @mouseenter="expandTags" @mouseleave="collapseTags">
      <div class="tags-container">
        <div class="channel-link" @click="navigateTo('/dynamic')">
          <i class="iconfont icon-dongtaiweixuanzhong"/>
          <span class="icon-title">动态</span>
        </div>
        <div class="channel-link" @click="navigateTo('//www.bilibili.com/v/popular/all')">
          <i class="iconfont icon-zhuti"/>
          <span class="icon-title">热门</span>
        </div>
        <el-tag
          v-for="(tag, index) in visibleTags"
          :key="index"
          class="channel-tag"
          @click="navigateToTag(tag)"
          effect="plain"
        >
          {{ tag }}
        </el-tag>
      </div>

<!--      <transition name="expand">-->
<!--        <div v-show="isExpanded" class="extra-tags">-->
<!--          <el-tag-->
<!--            v-for="(tag, index) in extraTags"-->
<!--            :key="index"-->
<!--            class="channel-tag"-->
<!--            @click="navigateToTag(tag)"-->
<!--            effect="plain"-->
<!--          >-->
<!--            {{ tag }}-->
<!--          </el-tag>-->
<!--        </div>-->
<!--      </transition>-->

<!--      <div class="toggle-icon" :class="{ 'is-expanded': isExpanded }">-->
<!--        <span class="icon-placeholder"></span>-->
<!--      </div>-->
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Tags } from '../const.js'

const router = useRouter()
const isExpanded = ref(false)
const visibleTags = ref(Tags.slice(0, 8)) // 默认显示前8个标签
const extraTags = ref([])

function navigateTo(url) {
  if (url.startsWith('//')) {
    window.open(`https:${url}`, '_blank')
  } else {
    router.push(url)
  }
}

function navigateToTag(tag) {
  router.push({
    path: '/channel',
    query: { tag }
  })
}

function expandTags() {
  isExpanded.value = true
  extraTags.value = Tags.slice(8)
}

function collapseTags() {
  isExpanded.value = false
  extraTags.value = []
}
</script>

<style lang="less" scoped>
.bili-header__channel {
  display: flex;
  align-items: center;
  padding: 0;
  height: 46px;
  background-color: #fff;
  border-bottom: 1px solid #e3e5e7;

  .primary-menu {
    padding: 0 10px;
    border-right: 1px solid #e3e5e7;

    .main-link {
      display: flex;
      align-items: center;
      gap: 4px;
      padding: 0 12px;
      height: 46px;
      color: #18191c;
      font-size: 14px;
      font-weight: 500;
      cursor: pointer;

      &:hover {
        color: #00aeec;
      }

      .icon-placeholder {
        width: 18px;
        height: 18px;
        background-color: #e3e5e7;
      }
    }
  }

  .channel-tags {
    flex: 1;
    height: 46px;
    padding: 0 20px;
    position: relative;

    .tags-container, .extra-tags {
      display: flex;
      flex-wrap: nowrap;
      align-items: center;
      height: 100%;
      gap: 12px;
    }

    .channel-link {
      display: flex;
      align-items: center;
      height: 32px;
      padding: 0 8px;
      font-size: 14px;
      color: #18191c;
      cursor: pointer;

      &:hover {
        color: #00aeec;
      }

      .icon-placeholder {
        width: 16px;
        height: 16px;
        margin-right: 4px;
        background-color: #e3e5e7;
      }
    }

    :deep(.channel-tag) {
      cursor: pointer;
      margin: 0;
      height: 32px;
      line-height: 32px;
      background-color: #f6f7f8;
      border-color: transparent;

      &:hover {
        color: #00aeec;
        background-color: #e3e5e7;
      }
    }

    .toggle-icon {
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
      display: flex;
      align-items: center;
      justify-content: center;
      width: 36px;
      height: 46px;
      cursor: pointer;
      color: #9499a0;
      background: linear-gradient(90deg, rgba(255,255,255,0) 0%, #fff 30%);

      &:hover {
        color: #00aeec;
      }

      &.is-expanded {
        .icon-placeholder {
          transform: rotate(180deg);
        }
      }

      .icon-placeholder {
        width: 16px;
        height: 16px;
        background-color: #9499a0;
        transition: transform 0.3s;
      }
    }
  }
}

.expand-enter-active,
.expand-leave-active {
  transition: max-height 0.3s ease-in-out, opacity 0.3s ease-in-out;
  max-height: 200px;
  overflow: hidden;
}

.expand-enter-from,
.expand-leave-to {
  max-height: 0;
  opacity: 0;
}
</style>
